/*

 */
package com.ohos.connent.accountcloud.biz.modular.dp.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ohos.connent.accountcloud.biz.modular.dp.enums.DpExceptionEnum;
import com.ohos.connent.accountcloud.biz.modular.dp.param.*;

import com.ohos.connent.accountcloud.biz.modular.dp.service.AuthDpService;
import com.ohos.connent.accountcloud.biz.modular.dp.service.DpUserService;
import com.ohos.connent.accountcloud.common.annotation.CommonLog;
import com.ohos.connent.accountcloud.common.exception.CommonException;
import com.ohos.connent.accountcloud.common.pojo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 人员控制器
 *
 *
 **/
@Api(tags = "人员控制器")
@ApiSupport(author = "OHOSCONNECT_TEAM", order = 9)
@RestController
@Validated
@RequestMapping("/api/v1/account/dp/user")
public class DpUserController {

    @Resource
    private DpUserService dpUserService;
    @Resource
    private AuthDpService authDpService;

    /**
     * 注册账号
     *
     *
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("注册账号")
    @CommonLog("注册账号")
    @PostMapping("/register")
    public CommonResult<String> register(@RequestBody @Valid DpUserRegisterParam dpUserRegisterParam) {
        if (BeanUtil.isEmpty(dpUserRegisterParam.getAccount())){
            throw new CommonException("账号不能为空！");
        }

        // 开启验证码则必须传入验证码
        if(ObjectUtil.isEmpty(dpUserRegisterParam.getValidCode())) {
            throw new CommonException(DpExceptionEnum.VALID_CODE_EMPTY.getValue());
        }
        // 开启验证码则必须传入验证码请求号
        if(ObjectUtil.isEmpty(dpUserRegisterParam.getValidCodeReqNo())) {
            throw new CommonException(DpExceptionEnum.VALID_CODE_REQ_NO_EMPTY.getValue());
        }
        // 执行校验验证码
        authDpService.validValidCode(dpUserRegisterParam.getPhone(), dpUserRegisterParam.getValidCode(), dpUserRegisterParam.getValidCodeReqNo());

        dpUserRegisterParam.setOrgId("1543842934270394371"); // 默认的分组待审核分组
        dpUserRegisterParam.setPositionId("1543899639134019584"); // 默认岗位

        // 默认为 企业 角色
        List<String> roleList = new ArrayList<>();
        roleList.add("1787371195281195009");
        dpUserRegisterParam.setRoleIdList(roleList);
        dpUserService.register(dpUserRegisterParam);
        return CommonResult.ok();
    }

    /**
     * 测试账号是否存在
     *
     *
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("检查账号是否存在")
    @CommonLog("检查账号是否存在")
    @PostMapping("/checkAccount")
    public CommonResult<String> checkAccount(@RequestBody @Valid DpUserAccountCheckParam dpUserAccountCheckParam) {
        if (BeanUtil.isEmpty(dpUserAccountCheckParam.getAccount())){
            throw new CommonException("账号不能为空！");
        }
        dpUserService.accountExist(dpUserAccountCheckParam);
        return CommonResult.ok();
    }


    @ApiOperationSupport(order = 3)
    @ApiOperation("根据手机号重置密码")
    @CommonLog("根据手机号重置密码")
    @PostMapping("/resetPasswordByPhone")
    public CommonResult<String> resetPasswordByPhone(@RequestBody @Valid DpUserResetPasswordParam dpUserResetPasswordParam) {
        authDpService.validValidCode(dpUserResetPasswordParam.getNumber(), dpUserResetPasswordParam.getValidCode(), dpUserResetPasswordParam.getValidCodeReqNo());
        dpUserService.resetPassword(dpUserResetPasswordParam);
        return CommonResult.ok();
    }

}
